package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.TodoMapper;
import com.itheima.pojo.ExceptionLog;
import com.itheima.pojo.Todo;
import com.itheima.service.TodoService;
import com.itheima.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.Map;

@Service
public class TodoServiceImpl implements TodoService {

    @Autowired
    private TodoMapper todoMapper;

    @Autowired
    private IdWorker idWorker;

    @Override
    public void add(Map map) {
        //将map集合中的字段转化成todo实体类
        Todo todo = JSON.parseObject(JSON.toJSONString(map), Todo.class);
        todo.setUid(idWorker.nextId()+"");
        todo.setCreateTime(new Date());  //todo adminUid需要通过token获取当前登录的用户的用户名,通过用户名获取uid
        todo.setUpdateTime(new Date());
        todoMapper.insertSelective(todo);
    }

    @Override
    public void delete(Map map) {
        //将map集合中的字段转化成todo实体类
        Todo todo = JSON.parseObject(JSON.toJSONString(map), Todo.class);
        todoMapper.deleteByPrimaryKey(todo);
    }

    @Override
    public void edit(Map map) {
        //将map集合中的字段转化成todo实体类
        Todo todo = JSON.parseObject(JSON.toJSONString(map), Todo.class);
        todo.setUpdateTime(new Date());  //todo adminUid需要通过token获取当前登录的用户的用户名,通过用户名获取uid
        todoMapper.updateByPrimaryKey(todo);
    }

    @Override
    public Page<Todo> getList(Map searchMap) {
        //获取当前页
        Integer currentPage = (Integer) searchMap.get("currentPage");
        if(null == currentPage || currentPage < 0){
            currentPage = 0;
        }
        //设置默认每页显示条数
        Integer pageSize = (Integer) searchMap.get("pageSize");
        if (null == pageSize || pageSize <= 0) {
            pageSize = 10;
        }
        PageHelper.startPage(currentPage,pageSize);
        Example example = this.createExample(searchMap);
        Page<Todo> page = (Page<Todo>) todoMapper.selectByExample(example);
        return page;
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(ExceptionLog.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            if(searchMap.get("uid")!=null && !"".equals(searchMap.get("uid"))){
                criteria.andLike("uid","%"+searchMap.get("uid")+"%");
            }
            if(searchMap.get("adminUid")!=null && !"".equals(searchMap.get("adminUid"))){
                criteria.andLike("adminUid","%"+searchMap.get("adminUid")+"%");
            }
            if(searchMap.get("text")!=null && !"".equals(searchMap.get("text"))){
                criteria.andLike("text","%"+searchMap.get("text")+"%");
            }
            if(searchMap.get("done")!=null){
                criteria.andEqualTo("done",searchMap.get("done"));
            }
            if(searchMap.get("status")!=null){
                criteria.andEqualTo("status",searchMap.get("status"));
            }
            if (searchMap.get("startTime")!=null){
                criteria.andGreaterThan("createTime",searchMap.get("startTime"));
            }
            if (searchMap.get("keyword")!=null){
                criteria.andLessThanOrEqualTo("createTime",searchMap.get("keyword"));
            }



        }
        return example;
    }
}
